using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;

using SypConst.Garbage;

using SypGui.ConnectionUC;
using SypGui.CreateForms;

namespace SypGui.ConnectionUC
{
    public partial class FtpStep2 : SypUCConnection
    {
        protected override void InitializeUC()
        {
            connMode = ConnectionMode.FTP_MODE;
        }

        public FtpStep2() : base()
        {
        }


        public FtpStep2(SetText outputMethod)
            : base (outputMethod)
        {
        }

        protected override void InitializeRessources()
        {
            base.InitializeRessources();
            FtpStep2SettingsBox.Text = GetRessources("WizardFtpSettings");
            //FtpStep2HostnameText.Text = GetRessources("WizardFtpHostname");
            //FtpStep2PasswordText.Text = GetRessources("WizardFtpPassword");
            //FtpStep2UserText.Text = GetRessources("WizardFtpUser");
            FtpStep2RemotePathText.Text = GetRessources("WizardFtpRemotePath");
            FtpStep2AnonymousCheck.Text = GetRessources("WizardFtpAnonymous");
            FtpStep2TestButton.Text = GetRessources("WizardTestButton");
        }

        public int FtpPortText
        {
            get
            {
                int port = -1;
                if (!int.TryParse(FtpStep2PortText.Text, out port))
                {
                    OutputMethod("[FTPSettings - PortText] wrong format for port", Output.ERROR);
                    return -1;
                }
                return port;
            }
            set
            {
                FtpStep2PortText.Text = value.ToString();
            }
        }


        public bool FtpAnonymousCheck
        {
            set
            {
                FtpStep2AnonymousCheck.Checked = value;
            }
            get
            {
                return FtpStep2AnonymousCheck.Checked;
            }
        }

        public string FtpPasswordText
        {
            get
            {
                return FtpStep2PasswordText.Text;
            }
            set
            {
                FtpStep2PasswordText.Text = value;
            }
        }

        public string FtpRemotePathText
        {
            get
            {
                return FtpStep2RemotePathText.Text;
            }
            set
            {
                FtpStep2RemotePathText.Text = value;
            }
        }

        public string FtpUserText
        {
            get
            {
                return FtpStep2UserText.Text;
            }
            set
            {
                FtpStep2UserText.Text = value;
            }
        }

        public string FtpHostnameText
        {
            get
            {
                return FtpStep2HostnameText.Text;
            }
            set
            {
                FtpStep2HostnameText.Text = value;
            }
        }

        public override void ProcessConfig()
        {
            if (FtpStep2RemotePathText.Text.Equals(GetRessources("WizardFtpRemotePath")))
                FtpStep2RemotePathText.Text = "/";
            string cmd = "Setconnection-site" + SypGarbage.COMMAND_SEPARATOR + SiteId + SypGarbage.COMMAND_SEPARATOR + (ushort)connMode + SypGarbage.COMMAND_SEPARATOR + "\"" + FtpStep2HostnameText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR  + FtpStep2PortText.Text  + SypGarbage.COMMAND_SEPARATOR + "\"" + FtpStep2PasswordText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR + "\"" + FtpStep2UserText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR + "\"" + FtpStep2RemotePathText.Text + "\"";
            OutputMethod(cmd, Output.OUT);
            OutputMethod(cmd, Output.COMMAND);
            base.ProcessConfig();
        }

        public override void Reset()
        {
            base.Reset();
            //FtpStep2HostnameText.Text = GetRessources("WizardFtpHostname");
            //FtpStep2PasswordText.Text = GetRessources("WizardFtpPassword");
            FtpStep2PortText.Text = SypGarbage.FTP_DEFAULT_PORT;
            FtpStep2AnonymousCheck.Checked = false;
            //FtpStep2UserText.Text = GetRessources("WizardFtpUser"); ;
        }


        private void TestFtpConnection()
        {
            /*
            FTPConnection SypFtpTest = new FTPConnection();
            try
            {
                int port = -1;
                string login = "";
                string password = "";
                if (!int.TryParse(FtpStep2PortText.Text, out port))
                {
                    MessageBox.Show(GetRessources("ConnectionError"));
                    return;
                }

                if (FtpStep2AnonymousCheck.Checked)
                {
                    login = "";
                    password = "";
                }
                else
                {
                    login = FtpStep2UserText.Text;
                    password = FtpStep2PasswordText.Text;
                }

                if (FtpStep2PassiveCheck.Checked)
                    SypFtpTest.Open(FtpStep2HostnameText.Text, port, login, password, FTPMode.Passive);
                else
                    SypFtpTest.Open(FtpStep2HostnameText.Text, port, login, password, FTPMode.Active);

                if (FtpStep2RemotePathText.Text != GetRessources("WizardFtpRemotePath"))
                    SypFtpTest.SetCurrentDirectory(FtpStep2RemotePathText.Text);
                //SypFtpTest.RemoveDir("blog");
                SypFtpTest.MakeDir("blog");
                SypFtpTest.RemoveDir("blog");
                //SypFtpTest.RemoveDir("blog");

                SypFtpTest.Close();
                MessageBox.Show(GetRessources("ConnectionValid"));
                SypGarbage.free(SypFtpTest);
            }
            catch (Exception IOE)
            {
                MessageBox.Show(GetRessources("ConnectionError") + " : "+ IOE.ToString());
                SypGarbage.free(SypFtpTest);
            }
             */
        }


        #region events

        private void FtpStep2HostnameText_Enter(object sender, EventArgs e)
        {
            FtpStep2HostnameText.Text = "";
        }

        private void FtpStep2PortText_Enter(object sender, EventArgs e)
        {
            FtpStep2PortText.Text = "";
        }

        private void FtpStep2PasswordText_Enter(object sender, EventArgs e)
        {
            FtpStep2PasswordText.Text = "";
        }

        private void FtpStep2UserText_Enter(object sender, EventArgs e)
        {
            FtpStep2UserText.Text = "";
        }

        private void FtpStep2TestButton_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Thread FtpTestThread = new Thread(new ThreadStart(TestFtpConnection));
            FtpTestThread.Start();

        }


        private void FTPStep2RemotePathText_TextChanged(object sender, EventArgs e)
        {
            FtpStep2RemotePathText.Text = "";
        }

        private void FtpStep2RemotePathText_Enter(object sender, EventArgs e)
        {
            FtpStep2RemotePathText.Text = "/";
        }

        #endregion

        private void FtpStep2AnonymousCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (FtpStep2AnonymousCheck.Checked)
            {
                FtpStep2PasswordText.Enabled = false;
                FtpStep2UserText.Enabled = false;
            }
            else
            {
                FtpStep2PasswordText.Enabled = true;
                FtpStep2UserText.Enabled = true;
            }
        }
    }
}
